(function() {
  $(document).ready(function() {
    var clientActivity, clientConnected, clientDisconnected, log, logAccount, refreshAccountinfo, refreshServerinfo, serverUrl, socket, statusMsg;
    log = function(msg) {
      return $('#log').prepend("" + msg + "<br />");
    };
    logAccount = function(msg) {
      return $('#account').append("" + msg + "<br />");
    };
    serverUrl = 'ws://192.168.1.76:9000/status';
    if (window.MozWebSocket) {
      socket = new MozWebSocket(serverUrl);
    } else if (window.WebSocket) {
      socket = new WebSocket(serverUrl);
    }
    socket.onopen = function(msg) {
      return $('#status').removeClass().addClass('online').html('connected');
    };
    socket.onmessage = function(msg) {
      var response;
      response = JSON.parse(msg.data);
      switch (response.action) {
        case "statusMsg":
          return statusMsg(response.data);
        case "clientConnected":
          return clientConnected(response.data);
        case "clientDisconnected":
          return clientDisconnected(response.data);
        case "clientActivity":
          return clientActivity(response.data);
        case "serverInfo":
          return refreshServerinfo(response.data);
        case "accountInfo":
          return refreshAccountinfo(response.data);
      }
    };
    socket.onclose = function(msg) {
      return $('#status').removeClass().addClass('offline').html('disconnected');
    };
    $('#status').click(function() {
      return socket.close();
    });
    statusMsg = function(msgData) {
      switch (msgData.type) {
        case "info":
          return log(msgData.text);
        case "warning":
          return log("<span class=\"warning\">" + msgData.text + "</span>");
      }
    };
    clientConnected = function(data) {
      $('#clientListSelect').append(new Option("" + data.ip + ":" + data.port, data.port));
      $("#accounts option[value='" + data.account + "']").removeClass('offline').addClass('online');
      return $('#clientCount').text(data.clientCount);
    };
    clientDisconnected = function(data) {
      $("#clientListSelect option[value='" + data.port + "']").remove();
      $("#accounts option[value='" + data.account + "']").removeClass('online').addClass('offline');
      return $('#clientCount').text(data.clientCount);
    };
    refreshServerinfo = function(serverinfo) {
      var ip, port, _ref, _results;
      $('#clientCount').text(serverinfo.clientCount);
      $('#maxClients').text(serverinfo.maxClients);
      $('#maxConnections').text(serverinfo.maxConnectionsPerIp);
      $('#maxRequetsPerMinute').text(serverinfo.maxRequetsPerMinute);
      _ref = serverinfo.clients;
      _results = [];
      for (port in _ref) {
        ip = _ref[port];
        _results.push($('#clientListSelect').append(new Option(ip + ':' + port, port)));
      }
      return _results;
    };
    clientActivity = function(port) {
      return $("#clientListSelect option[value='" + port + "']").css("color", "red").animate({
        opacity: 100
      }, 600, function() {
        return $(this).css("color", "black");
      });
    };
    refreshAccountinfo = function(accountinfo) {
      var account, i, _results, status,account;
      $('#accounts').html('');
      _results = [];
      for (var i = 0; i < accountinfo.length; i++) {
          account = accountinfo[i];
          status = (account['status'] == 1 ? 'online' : 'offline');
          _results.push($('#accounts').append('<option value="'+account['id']+'" class="'+status+'">'+account['name']+'</option>'));
      };
      /*for (i in accountinfo) {
        account = accountinfo[i];
        _results.push($('#accounts').append(new Option(account['name'], account['id'])));
      }*/
      return _results;
    };
  });
}).call(this);
